package com.tjsinfo.tjsoa.system.action;

import java.io.File;
import java.io.IOException;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.tjsinfo.tjsoa.addresslist.bo.IAddressListSystemUserBo;
import com.tjsinfo.tjsoa.addresslist.vo.AddressListSystemUser;
import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.fileExpress.bo.IFileExpressGroupBo;
import com.tjsinfo.tjsoa.fileExpress.bo.IFileExpressUserSizeBo;
import com.tjsinfo.tjsoa.fileExpress.vo.TjsUserEmail;
import com.tjsinfo.tjsoa.system.bo.ILianDongBO;
import com.tjsinfo.tjsoa.system.bo.ISystemUserBo;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;

/**
 * 添加用户
 * @author Administrator
 *
 */
public class AddUserAction extends Action{
	private ISystemUserBo systemUserBo;
	private IFileExpressUserSizeBo fileExpressUserSizeBo;
	private IFileExpressGroupBo fileExpressGroupBo;
	private IAddressListSystemUserBo addressListSystemUserBo;
	private ILianDongBO lianDongBo;

	public void setAddressListSystemUserBo(
			IAddressListSystemUserBo addressListSystemUserBo) {
		this.addressListSystemUserBo = addressListSystemUserBo;
	}

	public void setFileExpressGroupBo(IFileExpressGroupBo fileExpressGroupBo) {
		this.fileExpressGroupBo = fileExpressGroupBo;
	}

	public void setFileExpressUserSizeBo(
			IFileExpressUserSizeBo fileExpressUserSizeBo) {
		this.fileExpressUserSizeBo = fileExpressUserSizeBo;
	}

	public ISystemUserBo getSystemUserBo() {
		return systemUserBo;
	}

	public void setSystemUserBo(ISystemUserBo systemUserBo) {
		this.systemUserBo = systemUserBo;
	}

	public ILianDongBO getLianDongBo() {
		return lianDongBo;
	}

	public void setLianDongBo(ILianDongBO lianDongBo) {
		this.lianDongBo = lianDongBo;
	}

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		try {
			String loginname = request.getParameter("loginname");
			String password = request.getParameter("password");
			String username = request.getParameter("username");
			Integer orderNo =  Integer.parseInt(request.getParameter("orderNo")); 
			String email = request.getParameter("email")==null?null:request.getParameter("email");
			String departmentid = request.getParameter("departmentid"); 
			String dutyid = request.getParameter("dutyid");
			String postid = request.getParameter("postid")==null?"":request.getParameter("postid");; 
			String rloesGroupid = request.getParameter("rolesgroupid"); 
			String rolesid  = request.getParameter("rolesid");
			String shouquanid = request.getParameter("shouquanid");
			
			TSystemUser systemUser = new TSystemUser();
			if (request.getParameter("userdatabaseId") != null && !"".equals(request.getParameter("userdatabaseId"))) {
				Integer dataBaseId = Integer.parseInt(request.getParameter("userdatabaseId"));
				systemUser.setDataBaseId(dataBaseId);
			}
			if (request.getParameter("erjigongsiid") != null && !"".equals(request.getParameter("erjigongsiid"))) {
				Integer erjigongsiid = Integer.parseInt(request.getParameter("erjigongsiid"));
				systemUser.setSubUserId(erjigongsiid);
			}
			systemUser.setLoginName(loginname);
			systemUser.setPassword(password);
			systemUser.setUserName(username);
			systemUser.setEmail(email);
			Byte state = 0;
			systemUser.setState(state);
			systemUser.setOrderNo(orderNo);
			
			Integer id = systemUserBo.singerSystemUser(systemUser, departmentid, dutyid, postid, rloesGroupid,rolesid);
//			ResourceBundle bundle = ResourceBundle.getBundle("com.tjsinfo.tjsoa.fileExpress.properties.FileExpress");
//			String path = bundle.getString("fileupload");
			TSystemUser user = systemUserBo.findByIdSystemUser(id);
//			String uuid = user.getUuid();
//			try {
//				String strdos = "cmd /c md \""+path+"\\"+uuid+"\"";
//				Process process;
//				try {
//					process = Runtime
//							.getRuntime().exec(strdos);
//					process.waitFor();
//					
//					strdos = "cmd /c md \""+path+"\\"+uuid+"\\download\\\"";
//					process = Runtime
//					.getRuntime().exec(strdos);
//					process.waitFor();
//				} catch (IOException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				} catch (InterruptedException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			} catch (RuntimeException e1) {
//			}
			TjsUserEmail userEmail = new TjsUserEmail();
			userEmail.setUserEmailId(ToolKit.getUUID().replace("-", ""));
			userEmail.setTjsUser(user);
			userEmail.setMaxExpress(1048576);
			userEmail.setMaxReception(Long.valueOf(20971520));
			fileExpressUserSizeBo.save(userEmail);
			
			fileExpressGroupBo.insertGroup("我的接收文件夹", id);
			
			AddressListSystemUser addressListSystemUser = new AddressListSystemUser();
			addressListSystemUser.setUserId(id);
			addressListSystemUser.setUserName(user.getUserName());
			addressListSystemUserBo.save(addressListSystemUser);
			
			if(shouquanid!=null && !"".equals(shouquanid)){
				systemUserBo.shouQuan(id, Integer.parseInt(shouquanid));				
			}
			
			//如果选择了联动，将该用户添加到联动中去
			String liandongids = request.getParameter("liandongid");
			if(liandongids!=null && !"".equals(liandongids)){
				lianDongBo.addUser(liandongids,id,user.getUserName());
			}
			
			
			request.setAttribute("linkurl", "system/user/userlist.jsp?fanhui=true");
			request.setAttribute("message", "system.userSave.message");
			return mapping.findForward("success");
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			request.setAttribute("Exception", e);
			return mapping.findForward("failed");
		}
	}
	
}
